﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admintractor_ArticleEdit : System.Web.UI.Page
{
    ArticleBusiness article = new ArticleBusiness();
    private int id = Helper.getQuery("id");
    private string directory = System.Configuration.ConfigurationManager.AppSettings["FilesUpload"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        UsersEntities use = (UsersEntities)Session["user"];
        if (use != null) { lb_Author.Text = use._userName; }
        else { Response.Redirect("Login.aspx"); }
        if (!IsPostBack)
        {
            bindPanel();
            bindCategory();           
            if (id > 0)
            {
                ArticlesEntities a = article.getItem(id);
                lb_Panel.Visible = false;
                CheckBoxList1.Visible = false;
                lb_view.Visible = true;
                lb_view.Text = "<img src="+a._image+" width=\"250px\" height=\"200px\" />";
                but_add.Text = "Sửa";
                txtName.Text = a._name.Trim();
                txtSummary.Text = a._summary.Trim();
                FCKeditor1.Value = a._content;
                lb_Author.Text = a._author;
                lb_image.Text = a._image;
                drop_Category.SelectedValue = a._categoryID.ToString();
                if (a._status.Trim().Equals("True") || a._status.Trim().Equals("1"))
                {
                    cbStatus.Checked = false;
                }
                else
                {
                    cbStatus.Checked = true;
                }
            }
            else
            {
                but_add.Text = "Thêm mới";
                lb_Panel.Visible = true;
                CheckBoxList1.Visible = true;
                lb_view.Visible = false;
            }
        }
    }

    public void bindCategory()
    {
        CategoryBusiness c = new CategoryBusiness();
        ArrayList items = c.getCategory();
        drop_Category.DataSource = items;
        drop_Category.DataTextField = "_name";
        drop_Category.DataValueField = "_id";
        drop_Category.DataBind();
        drop_Category.Items.Add(new ListItem("- Chọn một -", 0.ToString()));
        drop_Category.SelectedValue = 0.ToString();
    }

    public void bindPanel()
    {
        PanelBusiness p = new PanelBusiness();
        ArrayList items = p.getPanel_Category(1);
        PanelesEntities entity =null;
        for (int i = 0; i < items.Count; i++)
        {
            entity=(PanelesEntities)items[i];
            ListItem item = new ListItem(entity._name, entity._id.ToString());
            CheckBoxList1.Items.Add(item);
            item = null; entity = null;
        }
    }

    private string panel_id()
    {
        string tmp = "";
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                tmp += CheckBoxList1.Items[i].Value + "@";
            }
        }
            return tmp;
    }
    private bool check()
    {
        bool flat = true;
        if (txtName.Text.Trim().Equals("") || FCKeditor1.Value.Trim().Equals(""))
        {
            flat = false;
        }
        return flat;
    }

    private bool upload(System.Web.UI.HtmlControls.HtmlInputFile htmlUpload, string filename)
    {
        bool flat = true;
        try
        {
            string strPath = System.IO.Path.GetFileName(filename);//dung dan o cung
            string serverPath = System.Web.HttpContext.Current.Server.MapPath(directory + "\\" + strPath);
            htmlUpload.PostedFile.SaveAs(serverPath);
        }
        catch (Exception)
        {
            flat = false;
        }
        return flat;
    }

    protected void but_add_Click(object sender, EventArgs e)
    {
        if (this.check())
        {
            ArticlesEntities item = new ArticlesEntities();
            item._name = txtName.Text.Trim();
            item._summary = txtSummary.Text.Trim();
            item._content = FCKeditor1.Value;
            item._status = cbStatus.Checked ? "0" : "1";
            item._author = lb_Author.Text;
            item._categoryID = int.Parse(drop_Category.SelectedValue.Trim());
            item._image = directory + "/" + FileUpload1.PostedFile.FileName;
            byte[] filecontent = new byte[FileUpload1.PostedFile.ContentLength];
            this.FileUpload1.PostedFile.InputStream.Read(filecontent, 0, filecontent.Length);

            if (id > 0)
            {
                item._articlesID = id;
                if (FileUpload1.PostedFile.FileName.Equals(""))
                {
                    item._image = lb_image.Text;
                    if (article.edit(item))
                    {
                        Response.Redirect("Article.aspx");
                    }
                    else
                    {
                        lb_Error.Text = "Xẩy ra nỗi trong qua trình thưc hiện";
                    }
                }
                else
                {
                    if (this.upload(FileUpload1, FileUpload1.PostedFile.FileName))
                    {
                        if (article.edit(item))
                        {
                            Response.Redirect("Article.aspx");
                        }
                        else
                        {
                            lb_Error.Text = "Xẩy ra nỗi trong qua trình thưc hiện";
                        }
                    }
                    else
                    {
                        lb_Error.Text = "Xẩy ra nỗi trong qua trình thưc hiện";
                    }
                }

            }
            else
            {
                if (!this.panel_id().Equals(""))
                {
                    item._panelID = this.panel_id();
                    if (FileUpload1.PostedFile.FileName.Equals(""))
                    {
                        item._image = "";
                        if (article.add(item))
                        {
                            Response.Redirect("Article.aspx");
                        }
                        else
                        {
                            lb_Error.Text = "Xẩy ra nỗi trong qua trình thưc hiện";
                        }
                    }
                    else
                    {
                        if (this.upload(FileUpload1, FileUpload1.PostedFile.FileName))
                        {
                            if (article.add(item))
                            {
                                Response.Redirect("Article.aspx");
                            }
                            else
                            {
                                lb_Error.Text = "Xẩy ra nỗi trong qua trình thưc hiện";
                            }
                        }
                        else
                        {
                            lb_Error.Text = "Xẩy ra nỗi trong qua trình thưc hiện";
                        }
                    }
                }
                else
                {
                    lb_Error.Text = "Chưa nhập đủ thông tin";
                }
            }
        }
        else
        {
            lb_Error.Text = "Chưa nhập đủ thông tin";
        }
    }
    protected void but_cancle_Click(object sender, EventArgs e)
    {
        Response.Redirect("Article.aspx");
    }
}
